package world;

import boot.MainBoot;
import com.bulletphysics.collision.broadphase.Dbvt.Node;
import com.bulletphysics.collision.shapes.CollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.material.Material;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.scene.Spatial;
import com.jme3.terrain.geomipmap.TerrainLodControl;
import com.jme3.terrain.geomipmap.TerrainQuad;
import com.jme3.terrain.heightmap.AbstractHeightMap;
import com.jme3.terrain.heightmap.ImageBasedHeightMap;
import com.jme3.texture.Texture;
import com.jme3.texture.Texture.WrapMode;
import java.util.ArrayList;
import java.util.List;
import jme3tools.converters.ImageToAwt;

/**
 *
 * @author adamgovan
 */
public class AreaManager {

    public RigidBodyControl landscape;
    public Spatial sceneModel;
    public ArrayList<String> maps = null;
    public RigidBodyControl terrainPhysicsNode;
    public TerrainQuad terrain;
    public CollisionShape sceneShape;
    //Materials
    public Material matRock;

    public AreaManager() {

        maps = new ArrayList<String>();
        maps.add("Scenes/town/main.scene");
    }

    public String loadArea(int i) {

        return maps.get(i);
    }
    //creates a wrap around sky for the game. Standard open GL pack
    //generated terrain, change the size to change the amount made
    //terrain to be built.
    public void createTerrain() {
        matRock = new Material(MainBoot.getMain().getAssetManager(), "Common/MatDefs/Terrain/TerrainLighting.j3md");
        matRock.setBoolean("useTriPlanarMapping", false);
        matRock.setBoolean("WardIso", true);
        matRock.setTexture("AlphaMap", MainBoot.getMain().getAssetManager().loadTexture("Textures/Terrain/splat/alphamap.png"));
        Texture heightMapImage = MainBoot.getMain().getAssetManager().loadTexture("Textures/Terrain/splat/mountains512.png");
        Texture grass = MainBoot.getMain().getAssetManager().loadTexture("Textures/Terrain/splat/grass.jpg");
        grass.setWrap(WrapMode.Repeat);
        matRock.setTexture("DiffuseMap", grass);
        matRock.setFloat("DiffuseMap_0_scale", 64);
        Texture dirt = MainBoot.getMain().getAssetManager().loadTexture("Textures/Terrain/splat/dirt.jpg");
        dirt.setWrap(WrapMode.Repeat);
        matRock.setTexture("DiffuseMap_1", dirt);
        matRock.setFloat("DiffuseMap_1_scale", 16);
        Texture rock = MainBoot.getMain().getAssetManager().loadTexture("Textures/Terrain/splat/road.jpg");
        rock.setWrap(WrapMode.Repeat);
        matRock.setTexture("DiffuseMap_2", rock);
        matRock.setFloat("DiffuseMap_2_scale", 128);
        Texture normalMap0 = MainBoot.getMain().getAssetManager().loadTexture("Textures/Terrain/splat/grass_normal.png");
        normalMap0.setWrap(WrapMode.Repeat);
        Texture normalMap1 = MainBoot.getMain().getAssetManager().loadTexture("Textures/Terrain/splat/dirt_normal.png");
        normalMap1.setWrap(WrapMode.Repeat);
        Texture normalMap2 = MainBoot.getMain().getAssetManager().loadTexture("Textures/Terrain/splat/road_normal.png");
        normalMap2.setWrap(WrapMode.Repeat);
        matRock.setTexture("NormalMap", normalMap0);
        matRock.setTexture("NormalMap_1", normalMap2);
        matRock.setTexture("NormalMap_2", normalMap2);

        AbstractHeightMap heightmap = null;
        try {
            heightmap = new ImageBasedHeightMap(ImageToAwt.convert(heightMapImage.getImage(), false, true, 0), 0.25f);
            heightmap.load();

        } catch (Exception e) {
            e.printStackTrace();
        }

        //max is 1025

        terrain = new TerrainQuad("terrain", 1025, 1025, heightmap.getHeightMap());
        List<Camera> cameras = new ArrayList<Camera>();
        cameras.add(MainBoot.getMain().getCamera());
        TerrainLodControl control = new TerrainLodControl(terrain, cameras);
        terrain.addControl(control);
        terrain.setMaterial(matRock);
        terrain.setLocalScale(new Vector3f(4, 4, 4));

        terrainPhysicsNode = new RigidBodyControl(CollisionShapeFactory.createMeshShape(terrain), 0);
        terrain.addControl(terrainPhysicsNode);
        MainBoot.getMain().getRootNode().attachChild(terrain);
        MainBoot.getMain().getBulletAppState().getPhysicsSpace().add(terrainPhysicsNode);
    }

    
}